package com.ttmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ttmall.common.pojo.EasyUIDataGridResult;
import com.ttmall.common.pojo.TaotaoResult;
import com.ttmall.mapper.TbItemParamMapper;
import com.ttmall.pojo.TbItemParam;
import com.ttmall.pojo.TbItemParamExample;
import com.ttmall.service.ItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by GalaIO on 2018/1/15.
 */
@Service
public class ItemParamServiceImpl implements ItemParamService {

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Override
    public EasyUIDataGridResult listItemParam(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        TbItemParamExample example = new TbItemParamExample();
        List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExampleWithBLOBs(example);

        EasyUIDataGridResult result = new EasyUIDataGridResult();
        PageInfo<TbItemParam> pageInfo = new PageInfo<TbItemParam>(tbItemParamList);
        result.setRows(tbItemParamList);
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    @Override
    public TaotaoResult getItemParamByItemCid(Long cid) {
        TaotaoResult result = TaotaoResult.ok();

        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(cid);
        List<TbItemParam> tbItemParams = tbItemParamMapper.selectByExampleWithBLOBs(example);
        if (tbItemParams != null && tbItemParams.size() > 0) {
            result.setData(tbItemParams.get(0));
        }

        return result;
    }

    @Override
    public TaotaoResult insertItemParam(Long cid, String paraData) {
        TbItemParam tbItemParam = new TbItemParam();
        Date now = new Date();
        tbItemParam.setItemCatId(cid);
        tbItemParam.setParamData(paraData);
        tbItemParam.setCreated(now);
        tbItemParam.setUpdated(now);
        tbItemParamMapper.insert(tbItemParam);
        return TaotaoResult.ok();
    }

    @Override
    public TaotaoResult deleteItemParam(List<Long> ids) {

        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tbItemParamMapper.deleteByExample(example);
        return TaotaoResult.ok();
    }


}
